/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.admin.Accounts;

/**
 *
 * @author Danh
 */
public class accountManager {

    Connection conn = null;

    public accountManager() {
        try {
            conn = MyConn.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Accounts> getall() {
        List<Accounts> list = new ArrayList<Accounts>();

        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Accounts");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Accounts a = new Accounts();
                a.setAccountId(rs.getInt("Account_Id"));
                a.setPassword(rs.getNString("User_Password"));
                a.setUsername(rs.getNString("User_Name"));
                a.setEmail(rs.getNString("User_Email"));
                a.setStatus(rs.getBoolean("Account_Status"));
                a.setRole(rs.getNString("Account_Role"));
                list.add(a);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    
    public boolean update(Accounts ac) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("UPDATE Accounts SET User_Password=?,User_Name=?,User_Email=?, Account_Status=?,Account_Role=? WHERE Account_Id=?");
            pstmt.setNString(1, ac.getPassword());
            pstmt.setNString(2, ac.getUsername());
            pstmt.setNString(3, ac.getEmail());
            pstmt.setBoolean(4, ac.isStatus());
            pstmt.setNString(5, ac.getRole());
            pstmt.setInt(6, ac.getAccountId());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
